package com.lj.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.Books;
import com.itmate.entity.Admins;
import com.itmate.entity.Users;
import com.lj.entity.BorrowBooks;

public class BorrowBooksmpl {
	public List<BorrowBooks> findBorrowBooksByBookId(int book_id){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("select * from borrowbooks where Book_id=?");
			ps.setInt(1, book_id);
			ResultSet rs = ps.executeQuery();
			BorrowBooks bb= null;
			List<BorrowBooks> list =new ArrayList<BorrowBooks>();
			while(rs.next()){
				bb=new BorrowBooks();
				bb.setBook_id(rs.getInt("Book_id"));
				bb.setBook_name(rs.getString("Book_name"));
				bb.setUser_id(rs.getInt("User_id"));
				bb.setUser_name(rs.getString("User_name"));
				bb.setBorrowdata(rs.getString("Borrowdata"));
				bb.setMaxstilldata(rs.getString("Maxstilldata"));
				list.add(bb);	
			}

			if(list!=null){
				return list;
			}else{
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public List<BorrowBooks> findBorrowBooksByUserId(int user_id){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("select * from borrowbooks where User_id=?");
			ps.setInt(1,user_id);
			ResultSet rs = ps.executeQuery();
			BorrowBooks bb= null;
			List<BorrowBooks> list =new ArrayList<BorrowBooks>();
			
			while(rs.next()){
			
				bb=new BorrowBooks();
				bb.setBook_id(rs.getInt("Book_id"));
				bb.setBook_name(rs.getString("Book_name"));
				bb.setUser_id(rs.getInt("User_id"));
				bb.setUser_name(rs.getString("User_name"));
				bb.setBorrowdata(rs.getString("Borrowdata"));
				bb.setMaxstilldata(rs.getString("Maxstilldata"));
				list.add(bb);	
			}
			if(list!=null){
				return list;
			}else{
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public int addBorrowBooks(BorrowBooks borrowbooks){
		Connection conn = null;
		PreparedStatement ps = null;
		int count = 0;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("insert into borrowbooks(Book_id,Book_name,User_id,User_name"
					+ ",Borrowdata,Maxstilldata) values(?,?,?,?,?,?)");
			ps.setInt(1, borrowbooks.getBook_id());
			ps.setString(2, borrowbooks.getBook_name());
			ps.setInt(3, borrowbooks.getUser_id());
			ps.setString(4, borrowbooks.getUser_name());
			ps.setString(5, borrowbooks.getBorrowdata());
			ps.setString(6, borrowbooks.getMaxstilldata());
			count = ps.executeUpdate();
			return count;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	public int DeleteBorrowBooksById(int user_id , int book_id){
		Connection conn = null;
		PreparedStatement ps = null;
		int count = 0;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("delete from borrowbooks where User_id=? and Book_id=?");
			ps.setInt(1, user_id);
			ps.setInt(2, book_id);
			count = ps.executeUpdate();
			return count;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	
	
	public Users findUser(String user_name ,String password){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("select * from users where User_name=? and Password=?");
			ps.setString(1,user_name);
			ps.setString(2,password);
			ResultSet rs = ps.executeQuery();
			Users u= null;			
			while(rs.next()){			
				u=new Users();
				u.setUser_id(rs.getInt("User_id"));			
			}
			if(u!=null){
				return u;
			}else{
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
	public Books findBooksById(int book_id){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = BaseDao.getConnection();
			ps = conn.prepareStatement("select * from books where book_id=?");
			ps.setInt(1,book_id);
			ResultSet rs=ps.executeQuery();
			Books b=null;
			while(rs.next()){
				b=new Books();
				b.setBook_id(rs.getInt(1));
				b.setBook_name(rs.getString(2));
				b.setWriter(rs.getString(3));
				b.setPress(rs.getString(4));
				b.setClassification(rs.getString(5));
				b.setPrice(rs.getDouble(6));
				b.setIsbn(rs.getString(7));
				b.setTotal(rs.getInt(8));
				b.setBorrow_count(rs.getString(9));		
			}
			if(b!=null){
				return b;
			}else{
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
}
